<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:nyext="urn:noury-extensions">
  <xsl:output method="text" encoding="UTF-8"/>

  <xsl:param name="package" />
  <xsl:param name="outdir" />
  <xsl:param name="name" />

  <xsl:include href="functions.xslt" />

  <xsl:template name="get-element-name">
    <xsl:param name="element" />
    <xsl:param name="prefix" />
    <xsl:param name="suffix" />
    <xsl:choose>
      <xsl:when test="$element/@shortName">
        <xsl:value-of select="concat($prefix, $element/@shortName, $suffix)" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat($prefix, $element/@name, $suffix)" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="value">
    <xsl:text>
        /// &lt;summary&gt;</xsl:text>
    <xsl:choose>
      <xsl:when test="string-length(.) &gt; 0">
        <xsl:value-of select="." />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="@name" />
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>&lt;/summary&gt;
#if (!NET_CF)
        [SRDescription("</xsl:text>
    <xsl:call-template name="get-element-name">
      <xsl:with-param name="element" select=".." />
    </xsl:call-template>
    <xsl:text>_</xsl:text>
    <xsl:value-of select="@name" />
    <xsl:text>")]
#endif
        [PersistentValue("</xsl:text>
    <xsl:value-of select="@code" />
    <xsl:text>")]
        </xsl:text>
    <xsl:value-of select="@name" />
    <xsl:if test="position()!=last()">
      <xsl:text>,</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match="enum">
    <xsl:text disable-output-escaping="yes">


    /// &lt;summary /&gt;
#if (!NET_CF)
    [TypeConverter(typeof(PersistentEnumConverter))]
#endif
    public enum </xsl:text>
    <xsl:call-template name="get-element-name">
      <xsl:with-param name="element" select="." />
    </xsl:call-template>
    <xsl:text>
    {</xsl:text>
    <xsl:apply-templates select="value" />
    <xsl:text>
    }</xsl:text>
  </xsl:template>
  
  <xsl:template name="generate-user-file">
    <xsl:text>// $Id$
/*! \file
 * \brief Definition of enumerations.
 *
 * \author $Author$
 * \date $Date$
 * \version $Revision$
 */

using System;
using Salamanca.DataAccess;
#if (!NET_CF)
using System.ComponentModel;
using System.Globalization;
using System.Resources;
#endif

namespace </xsl:text>
      <xsl:value-of select="$package" />
      <xsl:text>
{



#if (!NET_CF)
    ///////////////////////////////////////////////////////////////////////////
    ///
    /// &lt;summary&gt;Specifies a description for a property or event that lies in the resources of the project.&lt;/summary&gt;
    ///
    ///////////////////////////////////////////////////////////////////////////

    internal sealed class SRDescriptionAttribute:
        DescriptionAttribute
    {

        /// &lt;summary&gt;Initializes a new instance of the &lt;see cref="SRDescriptionAttribute" /&gt; class.&lt;/summary&gt;
        /// &lt;param name="name"&gt;The name of the string resource which contains the description of the property or of the event.&lt;/param&gt;
        public SRDescriptionAttribute(string name):
            base(name)
        {
            _Name=name;
        }

        /// &lt;summary&gt;Gets the description stored in this attribute.&lt;/summary&gt;
        public override string Description
        {
            get
            {
                if (!string.IsNullOrEmpty(_Name))
                {
                    try
                    {
                        string ret=Resources.SR.ResourceManager.GetString(_Name, CultureInfo.CurrentCulture);
                        if (!string.IsNullOrEmpty(ret))
                            return ret;
                    } catch (MissingManifestResourceException)
                    {
                    }
                }

                return base.Description;
            }
        }

        /// &lt;summary&gt;The name of the string resource which contains the description of the property or of the event.&lt;/summary&gt;
        private string _Name;
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// &lt;summary&gt;Formats enumerations according to their description.&lt;/summary&gt;
    ///
    ///////////////////////////////////////////////////////////////////////////

    public class DescriptionFormatter:
        ICustomFormatter,
        IFormatProvider
    {
        /// &lt;summary&gt;Constructor.&lt;/summary&gt;
        public DescriptionFormatter()
        {
        }

        /// &lt;summary&gt;Gets an object that defines how to format the specified type.&lt;/summary&gt;
        public object GetFormat(Type formatType)
        {
            if (formatType==typeof(ICustomFormatter))
                return this;

            return null;
        }

        /// &lt;summary&gt;Converts the value of a specified object to an equivalent string representation using specified format and culture-specific formatting information.&lt;/summary&gt;
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            if ((arg!=null) &amp;&amp; (arg is Enum) &amp;&amp; ((format==null) || (format=="G")))
            {
                DescriptionAttribute[] da=(DescriptionAttribute[])(arg.GetType().GetField(arg.ToString())).GetCustomAttributes(typeof(DescriptionAttribute), false);
                foreach (DescriptionAttribute v in da)
                    return v.Description;
            }

            if (arg is IFormattable)
                return ((IFormattable)arg).ToString(format, formatProvider);
            else if (arg!=null)
                return arg.ToString();

            return string.Empty;
        }

        /// &lt;summary&gt;Gets an instance of this formatter.&lt;/summary&gt;
        public static DescriptionFormatter Instance
        {
            get
            {
                if (_Instance==null)
                    _Instance=new DescriptionFormatter();
                return _Instance;
            }
        }

        /// &lt;summary&gt;An instance of this formatter.&lt;/summary&gt;
        private static DescriptionFormatter _Instance;

    }
#endif

}
</xsl:text>
  </xsl:template>
  
  <xsl:template name="generate-model-file">
    <xsl:variable name="src">
      <xsl:text>// $Id$
using System;
using Salamanca.DataAccess;
#if (!NET_CF)
using System.ComponentModel;
#endif

namespace </xsl:text>
      <xsl:value-of select="$package" />
      <xsl:text>
{
</xsl:text>
      <xsl:apply-templates select="/manifest/types/enum" />
      <xsl:text>

}
</xsl:text>
    </xsl:variable>
    <xsl:value-of select="nyext:generateFile($src, nyext:combinePath($outdir, concat(nyext:getModelFileName($name), '.cs')))" />
  </xsl:template>

  <xsl:template match="/">
    <xsl:call-template name="generate-user-file" />
    <xsl:call-template name="generate-model-file" />
  </xsl:template>
</xsl:stylesheet>
